package kr.co.kiso.usr.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

public class MailUtil {


	Logger log = Logger.getLogger(getClass());
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub

		String sTitle = "김형욱 테스트";
		String sContents = "This is a message body.\nHere's the second line. 한글포함";
		// 아래 인코딩 부분은 환경에 따라 제목의 한글이 깨질 경우 주석을 제거하세요.
		// sTitle = new String(sTitle.getBytes("euc-kr"),"8859_1");

		String to = "chejug@gmail.com";
		String from = "chejug@naver.com";
		String host = ConstValue.MAIL_SERVER_HOST;

		// create some properties and get the default Session
		Properties props = new Properties();
		props.put("mail.smtp.host", host);

		Session sess = Session.getDefaultInstance(props, null);

		
		try {
			// create a message
			Message msg = new MimeMessage(sess);
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] address = { new InternetAddress(to) };
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(sTitle);
			msg.setSentDate(new Date());
			msg.setContent(sContents + host, "text/html; charset=euc-kr"); // HTML
																			// 형식
			// msg.setText(sContents); // TEXT 형식

			Transport.send(msg);
		} catch (MessagingException mex) {
			
//        	log.error("error : ");
//        	log.error(mex.toString());

		}
	}
	
	public String sendMail(String title,String content, String toEamil, String fromEail){
		String sTitle = "infobee 구직 정보입니다";
		String sContents = "This is a message body.\nHere's the second line. 한글포함";
		// 아래 인코딩 부분은 환경에 따라 제목의 한글이 깨질 경우 주석을 제거하세요.
		// sTitle = new String(sTitle.getBytes("euc-kr"),"8859_1");

		String to = toEamil;
		String from = fromEail;
		String host = ConstValue.MAIL_SERVER_HOST;

		// create some properties and get the default Session
		Properties props = new Properties();
		props.put("mail.smtp.host", host);

		Session sess = Session.getDefaultInstance(props, null);

		try {
			// create a message
			Message msg = new MimeMessage(sess);
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] address = { new InternetAddress(to) };
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(title);
			msg.setSentDate(new Date());
//			msg.setContent(sContents + host, "text/html; charset=euc-kr"); // HTML
			msg.setContent(content, "text/html; charset=euc-kr"); // HTML
																			// 형식
			// msg.setText(sContents); // TEXT 형식

			Transport.send(msg);
			return "01";
		} catch (MessagingException mex) {
			
			return mex.getMessage();
		}
		
	}
}
